﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using InformationSystem.Framework.Dao;
using InformationSystem.BLL.Core.Model;
using Spring.Data.Generic;
using System.Data;
using Spring.Data.Common;

namespace InformationSystem.BLL.Core.Dao.Impl
{
    public class CollegeDaoImpl : InfoSysAdoDaoSupport, ICollegeDao
    {
        public IList<College> GetAllCollegeInfo()
        {
            string selectSql = @"select collegeid,collegeName,ispublicdept,userforum,forumStatus,forumpublictoAll from College";

            return AdoTemplate.QueryWithRowMapper<College>(CommandType.Text, selectSql, new UserRow<College>());

        }


        public College GetCollegeInfo(int collegeid)
        {
            string selectSql = @"select collegeid,collegeName,ispublicdept,userforum,forumStatus,forumpublictoAll from College where collegeid=@collegeid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(collegeid);
            try
            {
                return AdoTemplate.QueryForObject<College>(CommandType.Text, selectSql, new UserRow<College>(), builder.GetParameters());
            }
            catch (Exception)
            {
                return null;
            }
        }

        public bool UpdateCollegeInfo(Model.College college)
        {
            string updateSql = @"update College set ispublicdept=@ispublicdept, userforum=@userforum, forumStatus=@forumStatus, forumpublictoAll=@forumpublictoAll where collegeid=@collegeid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(college.CollegeId);
            builder.Create().Name("ispublicdept").Type(DbType.Boolean).Value(college.IsPublicDept);
            builder.Create().Name("userforum").Type(DbType.Boolean).Value(college.UserForum);
            builder.Create().Name("forumStatus").Type(DbType.Boolean).Value(college.ForumStatus);
            builder.Create().Name("forumpublictoAll").Type(DbType.Boolean).Value(college.ForumPublicToAll);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, updateSql, builder.GetParameters()) == 1;

        }

        internal class UserRow<T> : IRowMapper<T> where T : College, new()
        {
            public T MapRow(IDataReader reader, int rowNum)
            {
                T College = new T();

                College.CollegeId = reader.GetInt32(reader.GetOrdinal("collegeid"));
                College.CollegeName = reader.GetString(reader.GetOrdinal("collegeName"));
                College.IsPublicDept = reader.IsDBNull(reader.GetOrdinal("ispublicdept")) ? false : reader.GetBoolean(reader.GetOrdinal("ispublicdept"));
                College.UserForum = reader.IsDBNull(reader.GetOrdinal("userforum")) ? false : reader.GetBoolean(reader.GetOrdinal("userforum"));
                College.ForumStatus = reader.IsDBNull(reader.GetOrdinal("forumStatus")) ? true : reader.GetBoolean(reader.GetOrdinal("forumStatus"));
                College.ForumPublicToAll = reader.IsDBNull(reader.GetOrdinal("forumpublictoAll")) ? false : reader.GetBoolean(reader.GetOrdinal("forumpublictoAll"));

                return College;
            }
        }


        public IList<ClassInfo> GetClassInfoByAss(LoginUser user)
        {
            string selectSql = @"select classid,collegeid,gradeid,className,createuserid,createdatetime 
                                    from ClassInfo
                                    where collegeid=@collegeid and gradeid=@gradeid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(user.CollegeID);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(user.GradeID);
            return AdoTemplate.QueryWithRowMapper<ClassInfo>(CommandType.Text, selectSql, new ClassInfoRow<ClassInfo>(), builder.GetParameters());
        }

        public bool UpdateClassInfo(ClassInfo classinfo, LoginUser user)
        {
            string updateSql = "update ClassInfo set className=@className where classid=@classid and collegeid=@collegeid and gradeid=@gradeid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("className").Type(DbType.String).Value(classinfo.ClassName);
            builder.Create().Name("classid").Type(DbType.String).Value(classinfo.ClassID);
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(user.CollegeID);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(user.GradeID);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, updateSql, builder.GetParameters()) == 1;
        }

        public bool CreateClassInfo(ClassInfo newclass, LoginUser user)
        {
            string selectSql = "select count(classid) as idcount from ClassInfo where classid=@classid";
            string insertSql = "insert into  ClassInfo (classid,collegeid,gradeid,className,createuserid,createdatetime) values(@classid,@collegeid,@gradeid,@className,@createuserid,@createdatetime)";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("classid").Type(DbType.String).Value(newclass.ClassID);

            int count = (int)AdoTemplate.ClassicAdoTemplate.ExecuteScalar(CommandType.Text, selectSql, builder.GetParameters());
            if (count != 0)
            {
                newclass.InsertOK = false;
                newclass.Msg = "存在相同记录!";
                return false;
            }
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(user.CollegeID);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(user.GradeID);
            builder.Create().Name("className").Type(DbType.String).Value(newclass.ClassName);
            builder.Create().Name("createuserid").Type(DbType.String).Value(user.UserID);
            builder.Create().Name("createdatetime").Type(DbType.DateTime).Value(newclass.CreateDateTime);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, insertSql, builder.GetParameters()) == 1;
        }

        public bool DeleteClassInfoByID(string classid, LoginUser user)
        {
            string deleteSql = "delete from ClassInfo where classid=@classid and collegeid=@collegeid and gradeid=@gradeid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("classid").Type(DbType.String).Value(classid);
            builder.Create().Name("collegeid").Type(DbType.Int32).Value(user.CollegeID);
            builder.Create().Name("gradeid").Type(DbType.Int32).Value(user.GradeID);
            return AdoTemplate.ExecuteNonQuery(CommandType.Text, deleteSql, builder.GetParameters()) == 1;
        }

        public ClassInfo GetClassInfo(string classid)
        {
            string selectSql = @"select classid,collegeid,gradeid,className,createuserid,createdatetime 
                                    from ClassInfo
                                    where classid=@classid";
            IDbParametersBuilder builder = CreateDbParametersBuilder();
            builder.Create().Name("classid").Type(DbType.String).Value(classid);
            DataTable dt = AdoTemplate.ClassicAdoTemplate.DataTableCreateWithParams(CommandType.Text, selectSql, builder.GetParameters());
            if (dt.Rows.Count == 0) return null;
            ClassInfo classinfo = new ClassInfo();
            classinfo.ClassID = dt.Rows[0]["classid"].ToString();
            classinfo.CollegeID = (int)dt.Rows[0]["collegeid"];
            classinfo.GradeID = (int)dt.Rows[0]["gradeid"];
            return classinfo;
        }
        internal class ClassInfoRow<T> : IRowMapper<T> where T : ClassInfo, new()
        {
            public T MapRow(IDataReader reader, int rowNum)
            {
                T Classinfo = new T();

                Classinfo.ClassID = reader.GetString(reader.GetOrdinal("classid"));
                Classinfo.CollegeID = reader.GetInt32(reader.GetOrdinal("collegeid"));
                Classinfo.GradeID = reader.GetInt32(reader.GetOrdinal("gradeid"));
                Classinfo.ClassName = reader.GetString(reader.GetOrdinal("className"));
                Classinfo.CreateUserID = reader.GetString(reader.GetOrdinal("createuserid"));
                Classinfo.CreateDateTime = reader.GetDateTime(reader.GetOrdinal("createdatetime"));

                return Classinfo;
            }
        }




    }
}
